package com.mt.api.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mt.common.utils.PloverDateUtil;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.api.modules.domain.AppNews;
import com.mt.api.modules.domain.vo.AppNewsVo;
import com.mt.api.modules.mapper.AppNewsMapper;
import com.mt.api.modules.service.AppNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: plover
 * @Date: 2020/07/20/10:05 上午
 * @Description:
 */
@Service
@Slf4j
public class AppNewsServiceImpl extends ServiceImpl<AppNewsMapper, AppNews> implements AppNewsService {

    @Override
    public WebResponse queryNewsByBrandId() {
        LambdaQueryWrapper<AppNews> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(AppNews::getUpdateTime);
        List<AppNews> appNews = baseMapper.selectList(wrapper);
        List<AppNewsVo> appNewsVos = Lists.newArrayList();
        appNews.forEach(appNew -> {
            AppNewsVo appNewsVo = new AppNewsVo();
            appNewsVo.setId(appNew.getId());
            appNewsVo.setContent(appNew.getContent());
            appNewsVo.setCreateTime(PloverDateUtil.dateFormatyMdHms(appNew.getCreateTime()));
            appNewsVos.add(appNewsVo);
        });
        return WebResponseUtils.successList(appNewsVos);
    }

    @Override
    public boolean addNews(AppNews appNews) {
        baseMapper.insert(appNews);
        return true;
    }

}
